package com.kaizeli.website.service.impl;

import com.kaizeli.website.mapper.OutputValueMapper;
import com.kaizeli.website.pojo.entity.OutputValueDO;
import com.kaizeli.website.pojo.vo.OutputValueVO;
import com.kaizeli.website.service.OutputValueService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 产值服务实现类
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class OutputValueServiceImpl implements OutputValueService {
    
    private final OutputValueMapper outputValueMapper;
    
    @Override
    public Long save(OutputValueDO outputValueDO) {
        log.info("新增产值记录，员工ID：{}，项目编号：{}，账期：{}", 
            outputValueDO.getEmployeeId(), outputValueDO.getProjectCode(), outputValueDO.getAccountPeriod());
        outputValueMapper.insert(outputValueDO);
        return outputValueDO.getId();
    }
    
    @Override
    public Boolean update(OutputValueDO outputValueDO) {
        log.info("更新产值记录，ID：{}", outputValueDO.getId());
        return outputValueMapper.updateById(outputValueDO) > 0;
    }
    
    @Override
    public OutputValueVO getById(Long id) {
        log.info("根据ID查询产值记录，ID：{}", id);
        OutputValueDO outputValueDO = outputValueMapper.selectById(id);
        if (outputValueDO == null) {
            return null;
        }
        return convertToVO(outputValueDO);
    }
    
    @Override
    public Boolean deleteById(Long id) {
        log.info("根据ID删除产值记录，ID：{}", id);
        return outputValueMapper.deleteById(id) > 0;
    }
    
    @Override
    public List<OutputValueVO> listByEmployee(Long employeeId) {
        log.info("根据员工ID查询产值记录，员工ID：{}", employeeId);
        List<OutputValueDO> list = outputValueMapper.selectList(
            new com.baomidou.mybatisplus.core.conditions.query.QueryWrapper<OutputValueDO>()
                .eq("employee_id", employeeId)
                .orderByDesc("account_period")
        );
        return list.stream().map(this::convertToVO).collect(Collectors.toList());
    }
    
    @Override
    public List<OutputValueVO> listByProject(Long projectId) {
        log.info("根据项目ID查询产值记录，项目ID：{}", projectId);
        List<OutputValueDO> list = outputValueMapper.selectList(
            new com.baomidou.mybatisplus.core.conditions.query.QueryWrapper<OutputValueDO>()
                .eq("project_id", projectId)
                .orderByDesc("account_period")
        );
        return list.stream().map(this::convertToVO).collect(Collectors.toList());
    }
    
    @Override
    public List<OutputValueVO> listByProjectCode(String projectCode) {
        log.info("根据项目编号查询产值记录，项目编号：{}", projectCode);
        List<OutputValueDO> list = outputValueMapper.selectList(
            new com.baomidou.mybatisplus.core.conditions.query.QueryWrapper<OutputValueDO>()
                .eq("project_code", projectCode)
                .orderByDesc("account_period")
        );
        return list.stream().map(this::convertToVO).collect(Collectors.toList());
    }
    
    @Override
    public List<OutputValueVO> listByEmployeeAndPeriod(Long employeeId, String accountPeriod) {
        log.info("根据员工ID和账期查询产值记录，员工ID：{}，账期：{}", employeeId, accountPeriod);
        List<OutputValueDO> list = outputValueMapper.selectByEmployeeAndPeriod(employeeId, accountPeriod);
        return list.stream().map(this::convertToVO).collect(Collectors.toList());
    }
    
    @Override
    public List<OutputValueVO> listByProjectAndPeriod(String projectCode, String accountPeriod) {
        log.info("根据项目编号和账期查询产值记录，项目编号：{}，账期：{}", projectCode, accountPeriod);
        List<OutputValueDO> list = outputValueMapper.selectByProjectAndPeriod(projectCode, accountPeriod);
        return list.stream().map(this::convertToVO).collect(Collectors.toList());
    }
    
    @Override
    public BigDecimal sumOutputByEmployee(Long employeeId, String startPeriod, String endPeriod) {
        log.info("统计员工产值，员工ID：{}，开始账期：{}，结束账期：{}", employeeId, startPeriod, endPeriod);
        BigDecimal total = outputValueMapper.sumOutputByEmployee(employeeId, startPeriod, endPeriod);
        return total != null ? total : BigDecimal.ZERO;
    }
    
    @Override
    public BigDecimal sumOutputByProject(Long projectId, String startPeriod, String endPeriod) {
        log.info("统计项目产值，项目ID：{}，开始账期：{}，结束账期：{}", projectId, startPeriod, endPeriod);
        BigDecimal total = outputValueMapper.sumOutputByProject(projectId, startPeriod, endPeriod);
        return total != null ? total : BigDecimal.ZERO;
    }
    
    @Override
    public Map<String, Object> getOutputStatistics(String startPeriod, String endPeriod) {
        log.info("获取产值统计，开始账期：{}，结束账期：{}", startPeriod, endPeriod);
        Map<String, Object> statistics = new HashMap<>();
        
        // TODO: 实现具体的统计逻辑
        // 这里可以根据实际需求实现产值统计逻辑
        
        return statistics;
    }
    
    @Override
    public List<Map<String, Object>> getOutputDistribution(String projectCode, String accountPeriod) {
        log.info("获取产值分配，项目编号：{}，账期：{}", projectCode, accountPeriod);
        
        // TODO: 实现具体的分配逻辑
        // 这里可以根据实际需求实现产值分配逻辑
        
        return List.of();
    }
    
    /**
     * 转换为VO
     */
    private OutputValueVO convertToVO(OutputValueDO outputValueDO) {
        OutputValueVO vo = new OutputValueVO();
        vo.setId(outputValueDO.getId());
        vo.setEmployeeId(outputValueDO.getEmployeeId());
        vo.setEmployeeName(outputValueDO.getEmployeeName());
        vo.setEmployeeCode(outputValueDO.getEmployeeCode());
        vo.setEmployeeType(outputValueDO.getEmployeeType());
        vo.setProjectId(outputValueDO.getProjectId());
        vo.setProjectCode(outputValueDO.getProjectCode());
        vo.setProjectName(outputValueDO.getProjectName());
        vo.setBusinessType(outputValueDO.getBusinessType());
        vo.setAccountPeriod(outputValueDO.getAccountPeriod());
        vo.setProjectManager(outputValueDO.getProjectManager());
        vo.setDepartment(outputValueDO.getDepartment());
        vo.setProjectLocation(outputValueDO.getProjectLocation());
        vo.setJoinDate(outputValueDO.getJoinDate());
        vo.setProjectJoinDate(outputValueDO.getProjectJoinDate());
        vo.setProjectLeaveDate(outputValueDO.getProjectLeaveDate());
        vo.setResignationDate(outputValueDO.getResignationDate());
        vo.setHasOrder(outputValueDO.getHasOrder());
        vo.setUnitPriceWithTax(outputValueDO.getUnitPriceWithTax());
        vo.setWorkHours(outputValueDO.getWorkHours());
        vo.setDeduction(outputValueDO.getDeduction());
        vo.setMonthlyOutput(outputValueDO.getMonthlyOutput());
        vo.setMonthlyOutputNoTax(outputValueDO.getMonthlyOutputNoTax());
        vo.setSalaryCost(outputValueDO.getSalaryCost());
        vo.setSocialInsurance(outputValueDO.getSocialInsurance());
        vo.setOtherFees(outputValueDO.getOtherFees());
        vo.setCommercialInsurance(outputValueDO.getCommercialInsurance());
        vo.setEquipmentCost(outputValueDO.getEquipmentCost());
        vo.setCompensationAmount(outputValueDO.getCompensationAmount());
        vo.setTotalCost(outputValueDO.getTotalCost());
        vo.setCostRate(outputValueDO.getCostRate());
        vo.setApprovalStatus(outputValueDO.getApprovalStatus());
        vo.setApproverId(outputValueDO.getApproverId());
        vo.setApproverName(outputValueDO.getApproverName());
        vo.setRemarks(outputValueDO.getRemarks());
        return vo;
    }
}

